package com.wdf.dota;

import java.io.IOException;
import java.io.InputStream;

import android.app.TabActivity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;

public class ViewActivity extends TabActivity {

	public TabHost tabHost;
	public static final String TAB_HERO="英雄介绍";
	public static final String TAB_SKILL="技能说明";
	public static final String TAB_LEVEL="推荐加点";
	public static final String TAB_EQUI="核心装备";
	public static final String TAB_DISS="刀友评论";
	
	Button submitCommentBtn;
	
	Button returnCommentBtn;
	
	public RadioGroup radioGroup;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.heroview_tab);
		
		submitCommentBtn =  (Button) findViewById(R.id.submit_comment);
		submitCommentBtn.setOnClickListener(submitCommentButtonListener);
		
		
		returnCommentBtn =  (Button) findViewById(R.id.return_comment);
		returnCommentBtn.setOnClickListener(returnCommentButtonListener);
		Bundle bundle = this.getIntent().getExtras();
		String prop = bundle.getString("heroprop");
		String heroname =	bundle.getString("heroname");
		try {
		
			
          tabHost=this.getTabHost();
        
        TabSpec ts1=tabHost.newTabSpec(TAB_HERO).setIndicator(TAB_HERO);
        Intent intent1 = new Intent(ViewActivity.this,HeroInfoActivity.class);
        intent1.putExtras(bundle);
        ts1.setContent(intent1);
        tabHost.addTab(ts1);
        
        TabSpec ts2=tabHost.newTabSpec(TAB_SKILL).setIndicator(TAB_SKILL);
        Intent intent2 = new Intent(ViewActivity.this,HeroSkillActivity.class);
        intent2.putExtras(bundle);
        ts2.setContent(intent2);
        tabHost.addTab(ts2);
        
        TabSpec ts3=tabHost.newTabSpec(TAB_LEVEL).setIndicator(TAB_LEVEL);
        Intent intent3 = new Intent(ViewActivity.this,HeroJiadianActivity.class);
        intent3.putExtras(bundle);
        ts3.setContent(intent3);
        tabHost.addTab(ts3);
        
        TabSpec ts4=tabHost.newTabSpec(TAB_EQUI).setIndicator(TAB_EQUI);
        Intent intent4 = new Intent(ViewActivity.this,HeroEquiActivity.class);
        intent4.putExtras(bundle);
        ts4.setContent(intent4);
        tabHost.addTab(ts4);
        
        TabSpec ts5=tabHost.newTabSpec(TAB_DISS).setIndicator(TAB_DISS);
        Intent intent5 = new Intent(ViewActivity.this,HeroDiscussionActivity.class);
        intent5.putExtras(bundle);
        ts5.setContent(intent5);
        tabHost.addTab(ts5);
        
        this.radioGroup=(RadioGroup)findViewById(R.id.main_radio);
        radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			public void onCheckedChanged(RadioGroup group, int checkedId) {
				
				switch(checkedId){
				case R.id.radio_button0:
					setStyle(R.id.radio_button0);
					tabHost.setCurrentTabByTag(TAB_HERO);
					break;
				case R.id.radio_button1:
					setStyle(R.id.radio_button1);
					tabHost.setCurrentTabByTag(TAB_SKILL);
					break;
				case R.id.radio_button2:
					setStyle(R.id.radio_button2);
					tabHost.setCurrentTabByTag(TAB_LEVEL);
					break;
				case R.id.radio_button3:
					setStyle(R.id.radio_button3);
					tabHost.setCurrentTabByTag(TAB_EQUI);
					break;
				case R.id.radio_button4:
					setStyle(R.id.radio_button4);
					tabHost.setCurrentTabByTag(TAB_DISS);
					break;
				}
			}
		});
   
		} catch (Exception e) {
			e.printStackTrace();
		}
		

		 ImageView  heromap= (ImageView ) findViewById(R.id.heromap);
			InputStream fis;
			try {
				fis = getAssets().open( "dota/"+heroname +".png");
				Bitmap bm = BitmapFactory.decodeStream(fis);
				heromap.setImageBitmap(bm);
			} catch (IOException e) {
				e.printStackTrace();
			}
			
			TextView heroProp= (TextView) findViewById(R.id.heroprop);
			heroProp.setText(Html.fromHtml(prop));
       
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
	            super.onActivityResult(requestCode, resultCode, data);
	            tabHost.getCurrentTabView().refreshDrawableState();
	}
	
	public void setStyle(int id){
		RadioButton rb = (RadioButton)findViewById(id);

	}
	
	OnClickListener returnCommentButtonListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			ViewActivity.this.finish();
		}
	};
	
	OnClickListener submitCommentButtonListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			   SharedPreferences sharedPreferences = getSharedPreferences("wonderful",
						Context.MODE_PRIVATE);
			   String accountid= sharedPreferences.getString("accountid", "0");
			   String username = sharedPreferences.getString("username", "");
			   
             if("0".equals(accountid) || username ==null || username.length() <=0){
            	 Intent intent = new Intent(ViewActivity.this,
            			 LoginActivity.class);
            	  startActivityForResult(intent, 4);
             }else{
            		Intent intent = new Intent(ViewActivity.this,
        					SubmitCommentActivity.class);
        			Bundle bundle = ViewActivity.this.getIntent().getExtras();
        			intent.putExtras(bundle);
        			startActivityForResult(intent, 4);
             }
		
		}
	};

}
